#ifndef __HTML_WINDOW_SINK_H__
#define __HTML_WINDOW_SINK_H__

#include "resource.h"
class ATL_NO_VTABLE CHtmlWindowSink:
	public CComObjectRootEx<CComSingleThreadModel>,
	public IDispatch
{
public:
	CComPtr<IWebBrowser2>   m_spWebBrowser;
	ZEmbededBrowser         *m_winBrowserHost;
	CHtmlWindowSink(): m_spWebBrowser (NULL)
	{}

BEGIN_COM_MAP(CHtmlWindowSink)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY_IID(DIID_HTMLWindowEvents2, IDispatch)
END_COM_MAP()

// IDispatch
public:
	STDMETHOD (GetTypeInfoCount) (UINT* pctinfo)
	{
		return	E_NOTIMPL;
	}

	STDMETHOD (GetTypeInfo) (UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
	{
		return	E_NOTIMPL;
	}
	STDMETHOD (GetIDsOfNames) (REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgdispid)
	{
		return	E_NOTIMPL;
	}
	STDMETHOD (Invoke) (DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags, 
		DISPPARAMS* pdispparams, 
		VARIANT* pVarResult, EXCEPINFO* pexcepinfo, 
		UINT* puArgErr)
	{
		IDispatch *pDispatch = NULL;
		HRESULT hr;
		UINT nArgIndex =pdispparams->cArgs-1;
		CString str;
		str.Format("focus wnd %x\n",::GetFocus());
		if (!pVarResult)
		{
			return E_POINTER;
		}
		switch(dispidMember)
		{
		case DISPID_HTMLWINDOWEVENTS2_ONBLUR:
			pDispatch = (IDispatch *)(pdispparams->rgvarg[nArgIndex--].punkVal);
			OutputDebugString(str);
			if( m_spWebBrowser == pDispatch ){
				if( !m_winBrowserHost->HitOnButtonBlue()){
					m_winBrowserHost->PostMessage (WM_CLOSE);
				}
			}
			break;
		case DISPID_HTMLWINDOWEVENTS2_ONFOCUS:
			SHANDLE_PTR hw;
			hr = m_spWebBrowser->get_HWND(&hw);
			pDispatch = (IDispatch *)(pdispparams->rgvarg[nArgIndex--].punkVal);
			OutputDebugString("DISPID_HTMLWINDOWEVENTS2_ONFOCUS\n");
			break;
		default:
			return DISP_E_MEMBERNOTFOUND;
		}
		return NOERROR;	
	}
};

#endif